package ui.gui;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

import domein.DomeinController;

public class AnderePanel extends JPanel implements ActionListener {

	private ButtonGroup btnGroup;
	private JToggleButton btnStartPositie, btnEindPositie;
	private String[][] obstakels;
	
	public AnderePanel(ButtonGroup btnGroup) {
		this.btnGroup = btnGroup;
		
		this.setLayout(new FlowLayout());
		
		this.obstakels = DomeinController.getInstance().geefObstakels();
		
		this.initComponents();
	}
	
	private void initComponents() {
		btnStartPositie = new JToggleButton("Startpositie", createIcon("assets/Textures/start.png"));
		btnStartPositie.setVerticalTextPosition(AbstractButton.BOTTOM);
		btnStartPositie.setHorizontalTextPosition(AbstractButton.CENTER);
		btnStartPositie.addActionListener(this);
		btnStartPositie.setToolTipText("Startpositie");
		
		btnEindPositie = new JToggleButton("Eindpositie", createIcon("assets/Textures/exit.png"));
		btnEindPositie.setVerticalTextPosition(AbstractButton.BOTTOM);
		btnEindPositie.setHorizontalTextPosition(AbstractButton.CENTER);
		btnEindPositie.addActionListener(this);
		btnEindPositie.setToolTipText("Eindpositie");
		
		btnGroup.add(btnStartPositie);
		btnGroup.add(btnEindPositie);
		
		add(btnStartPositie);
		add(btnEindPositie);
	}
	
	private ImageIcon createIcon(String path) {	
		BufferedImage drawImage = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);
		
		try {
			BufferedImage bImage = ImageIO.read(new File(path));
			
			Graphics g = drawImage.getGraphics();
			g.drawImage(new ImageIcon(bImage).getImage(), 0, 0, 64, 64, null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return new ImageIcon(drawImage);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnStartPositie) {
			DomeinController.getInstance().selecteerStartpositie();
		}
		else if(e.getSource() == btnEindPositie) {
			DomeinController.getInstance().selecteerEindpositie();
		}
	}
}
